package ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;

import control.Operator;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

public class DelBookUI extends JFrame {

	private JTabbedPane tabbedPane;
	private JPanel panel1, panel2;

	private JTextField textField1, textField2;
	private JButton button;
	private JButton button_1;

	/**
	 * Create the frame.
	 */
	public DelBookUI() {
		setTitle("删除图书");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(450, 250, 500, 350);
		setLocationRelativeTo(null); // 居中显示

		// 主面板
		JPanel mainPanel = new JPanel();
		mainPanel.setBorder(new EmptyBorder(15, 15, 15, 15));
		mainPanel.setLayout(new BorderLayout(0, 10));
		setContentPane(mainPanel);

		// 标题
		JLabel titleLabel = new JLabel("删除图书", SwingConstants.CENTER);
		titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
		titleLabel.setForeground(new Color(70, 130, 180));
		mainPanel.add(titleLabel, BorderLayout.NORTH);

		// Tab面板
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		mainPanel.add(tabbedPane, BorderLayout.CENTER);

		// 初始化面板
		initPanel1();
		initPanel2();

		// 添加标签页
		tabbedPane.addTab("按书名删除", panel1);
		tabbedPane.addTab("按作者名删除", panel2);

		setVisible(true);
	}

	private void initPanel1() {
		panel1 = new JPanel();
		panel1.setLayout(new BorderLayout(0, 15));
		panel1.setBorder(new EmptyBorder(20, 20, 20, 20));

		// 表单区域
		JPanel formPanel1 = new JPanel();
		formPanel1.setLayout(null);
		formPanel1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"),
				"删除条件", TitledBorder.LEADING, TitledBorder.TOP,
				new Font("微软雅黑", Font.PLAIN, 14), new Color(100, 100, 100)));
		panel1.add(formPanel1, BorderLayout.CENTER);

		JLabel label1 = new JLabel("书名：");
		label1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		label1.setBounds(80, 50, 60, 25);
		formPanel1.add(label1);

		textField1 = new JTextField();
		textField1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		textField1.setBounds(150, 50, 200, 30);
		formPanel1.add(textField1);
		textField1.setColumns(10);

		// 按钮区域
		JPanel buttonPanel1 = new JPanel();
		buttonPanel1.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));
		panel1.add(buttonPanel1, BorderLayout.SOUTH);

		JButton yesbutton1 = new JButton("确定");
		yesbutton1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		yesbutton1.setBackground(new Color(70, 130, 180));
		yesbutton1.setForeground(Color.WHITE);
		yesbutton1.setFocusPainted(false);
		yesbutton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String bookname = textField1.getText().trim();
				if (bookname.isEmpty()) {
					JOptionPane.showMessageDialog(null, "书名不能为空！", "输入错误", JOptionPane.WARNING_MESSAGE);
					return;
				}

				Operator operator = new Operator();
				boolean isSuccess = operator.delBookByBookname(bookname);
				if(isSuccess){
					JOptionPane.showMessageDialog(null, "删除成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
				}else{
					JOptionPane.showMessageDialog(null, "删除失败！", "失败", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		buttonPanel1.add(yesbutton1);

		JButton rtnbutton1 = new JButton("返回");
		rtnbutton1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		rtnbutton1.setBackground(new Color(220, 220, 220));
		rtnbutton1.setFocusPainted(false);
		rtnbutton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DelBookUI.this.dispose();
			}
		});
		buttonPanel1.add(rtnbutton1);
	}

	private void initPanel2() {
		panel2 = new JPanel();
		panel2.setLayout(new BorderLayout(0, 15));
		panel2.setBorder(new EmptyBorder(20, 20, 20, 20));

		// 表单区域
		JPanel formPanel2 = new JPanel();
		formPanel2.setLayout(null);
		formPanel2.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"),
				"删除条件", TitledBorder.LEADING, TitledBorder.TOP,
				new Font("微软雅黑", Font.PLAIN, 14), new Color(100, 100, 100)));
		panel2.add(formPanel2, BorderLayout.CENTER);

		JLabel label2 = new JLabel("作者名：");
		label2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		label2.setBounds(80, 50, 70, 25);
		formPanel2.add(label2);

		textField2 = new JTextField();
		textField2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		textField2.setBounds(160, 50, 200, 30);
		formPanel2.add(textField2);
		textField2.setColumns(10);

		// 按钮区域
		JPanel buttonPanel2 = new JPanel();
		buttonPanel2.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));
		panel2.add(buttonPanel2, BorderLayout.SOUTH);

		JButton yesbutton2 = new JButton("确定");
		yesbutton2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		yesbutton2.setBackground(new Color(70, 130, 180));
		yesbutton2.setForeground(Color.WHITE);
		yesbutton2.setFocusPainted(false);
		yesbutton2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String author = textField2.getText().trim();
				if (author.isEmpty()) {
					JOptionPane.showMessageDialog(null, "作者名不能为空！", "输入错误", JOptionPane.WARNING_MESSAGE);
					return;
				}

				Operator operator = new Operator();
				boolean isSuccess = operator.delBookByAuthor(author);
				if(isSuccess){
					JOptionPane.showMessageDialog(null, "删除成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
				}else{
					JOptionPane.showMessageDialog(null, "删除失败！", "失败", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		buttonPanel2.add(yesbutton2);

		JButton rtnbutton2 = new JButton("返回");
		rtnbutton2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		rtnbutton2.setBackground(new Color(220, 220, 220));
		rtnbutton2.setFocusPainted(false);
		rtnbutton2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DelBookUI.this.dispose();
			}
		});
		buttonPanel2.add(rtnbutton2);
	}
}
